<?php
namespace App\Service\Admin;

class WorkhourService extends BaseService
{
    //数据列表
    public static function index($pas, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbConfigWorkHour)
            ->where('del', 0);
        // if ($pas['role_id']) {
        //     $db = $db->where('role_id', $pas['role_id']);
        // }
        // if ($pas['depart_id']) {
        //     $db = $db->where('depart_id', $pas['depart_id']);
        // }
        if ($pas['keyword']) {
            $db = $db->where('title', 'like', '%'.$pas['keyword'].'%');
        }
        if ($pas['sort_id'] == 2) {
            $db = $db->orderBy('uptime', 'desc');
        } else {
            $db = $db->orderBy('uptime', 'asc');
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $arys = [];
        foreach ($res as $v) {
            $cntOnes = self::dbConn(self::$dbConfigWhCnt)
                ->where('del', 0)
                ->where('wh_id', $v->id)
                ->get();
            $cnts = [];
            if (count($cntOnes)) {
                foreach ($cntOnes as $vv) {
                    $cnts[] = [
                        'id' => $vv->id, 'cnt' => $vv->cnt,
                    ];
                }
            }
            $arys[] = [
                'id' => $v->id, 'title' => $v->title,
                'lasttime' => $v->uptime,
                'cnts' => $cnts,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //选项
    public static function getOption()
    {
        $arys = ['type' => []];
        $types = self::$whTypes;
        foreach ($types as $k => $v) {
            $arys['type'][] = [
                'id' => $k, 'title' => $v,
            ];
        }
        return self::toback(0, '', $arys);
    }

    //新增
    public static function add($pas)
    {
        $pas['addtime'] = $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbConfigWorkHour)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //修改
    public static function modify($id, $pas)
    {
        $res = self::dbConn(self::$dbConfigWorkHour)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbConfigWorkHour)->where('id', $id)->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //删除
    public static function del($id)
    {
        $res = self::dbConn(self::$dbConfigWorkHour)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbConfigWorkHour)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //内容修改
    public static function cntModify($id, $pas)
    {
        $res = self::dbConn(self::$dbConfigWhCnt)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        $pas['uptime'] = toDate();
        if (empty($res)) {
            $pas['addtime'] = toDate();
            self::dbConn(self::$dbConfigWhCnt)->insertGetId($pas);
        } else {
            self::dbConn(self::$dbConfigWhCnt)
                ->where('id', $id)
                ->update($pas);
        }
        return self::toback();
    }

    //内容删除
    public static function cntDel($id)
    {
        $res = self::dbConn(self::$dbConfigWhCnt)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbConfigWhCnt)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
}